<template>
	<view class="container">
		<view class="intro">
			<image src="/static/images/login/logo.png"></image>
			<view class="tips">
				彩姿Color
				<br>
				质感生活，从内衣开始。
			</view>
		</view>
		<view class="bottom">
			<button type="primary" size="default" class="login-btn" lang="zh_CN" @tap="oneClickLogin">
				<image src="/static/images/mine/wechat.png"></image>
				微信一键登录
			</button>
		</view>
	</view>
</template>

<script>
	import {
		mapMutations
	} from 'vuex'

	export default {
		data() {
			return {
				jsCode: '',
				avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
			}
		},
		onLoad() {
			// 如果有token
			if (uni.getStorageSync('token')) {
				uni.switchTab({
					url: '/pages/checkIty/checkIty'
				})
			}
		},
		methods: {
			onChooseAvatar(e) {
				const {
					avatarUrl
				} = e.detail
				this.setData({
					avatarUrl,
				})
			},
			// 一键登录
			oneClickLogin() {
				uni.login({ // 获取jscode
					provider: 'weixin',
					success: (res) => {
						this.jsCode = res.code
						console.log('1. 获取jscode', res)
						this.$newApi.getOpenId({ // 用jscode换取openid
							code: res.code,
						}).then(res => {
							console.log('2. 获取session_key,openid', res)
							this.handleLoginInfo(res)
						})
					}
				})
			},
			//登录后的信息处理
			handleLoginInfo(res) {
				// 处理token
				uni.setStorageSync('openid', res.data.openid)
				uni.setStorageSync('token', res.data.token)

				uni.showToast({
					title: '登录成功',
					icon: 'none',
					duration: 800
				})

				setTimeout(() => {
					//跳转首页
					uni.switchTab({
						url: '/pages/checkIty/checkIty'
					})
				}, 800)
			},
		},
	}
</script>

<style lang="scss" scoped>
	.intro {
		width: 100%;
		height: 60vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: space-evenly;
		font-size: $font-size-base;
		color: $text-color-assist;

		image {
			width: 165rpx;
			height: 165rpx;
		}

		.tips {
			line-height: 72rpx;
			text-align: center;
		}
	}

	.bottom {
		height: 40vh;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		padding: 0 40rpx;

		.login-btn {
			width: 100%;
			border-radius: 50rem !important;
			display: flex;
			align-items: center;
			justify-content: center;
			padding: 10rpx 0;

			image {
				width: 36rpx;
				height: 30rpx;
				margin-right: 10rpx;
			}
		}

		.row {
			.grid {
				width: 20%;

				image {
					width: 60rpx;
					height: 60rpx;
					margin-bottom: 10rpx;
				}
			}
		}
	}
</style>